package com.cl.spark.dto;

import com.alibaba.fastjson.JSONArray;
import lombok.Data;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;

@Data
public class SparkResult {
    private String hdfsPath;
    private Dataset<Row> dataset;
    private JSONArray data;
    private Integer total;
    private String msg;
    private Integer code;
    private JSONArray fields;

    public static SparkResult success(Dataset<Row> dataset) {
        SparkResult sparkResult = new SparkResult();
        sparkResult.setDataset(dataset);
        sparkResult.setTotal((int) dataset.count());
        sparkResult.setCode(200);
        return sparkResult;
    }
    public static SparkResult success(JSONArray data) {
        SparkResult sparkResult = new SparkResult();
        sparkResult.setData(data);
        sparkResult.setTotal(data.size());
        sparkResult.setCode(200);
        return sparkResult;
    }
    public static SparkResult success(JSONArray data,JSONArray fields) {
        SparkResult sparkResult = new SparkResult();
        sparkResult.setData(data);
        sparkResult.setFields(fields);
        sparkResult.setTotal(data.size());
        sparkResult.setCode(200);
        return sparkResult;
    }
    public static SparkResult success(JSONArray data,Integer total) {
        SparkResult sparkResult = new SparkResult();
        sparkResult.setData(data);
        sparkResult.setTotal(total);
        sparkResult.setCode(200);
        return sparkResult;
    }
    public static SparkResult success(String hdfsPath,Integer total) {
        SparkResult sparkResult = new SparkResult();
        sparkResult.setHdfsPath(hdfsPath);
        sparkResult.setTotal(total);
        sparkResult.setCode(200);
        return sparkResult;
    }
    public static SparkResult success(String hdfsPath,Integer total,String msg) {
        SparkResult sparkResult = new SparkResult();
        sparkResult.setHdfsPath(hdfsPath);
        sparkResult.setTotal(total);
        sparkResult.setMsg(msg);
        sparkResult.setCode(200);
        return sparkResult;
    }
    public static SparkResult failed(String msg) {
        SparkResult sparkResult = new SparkResult();
        sparkResult.setMsg(msg);
        sparkResult.setCode(500);
        return sparkResult;
    }
}
